#include "scheduler.h"
uint8_t task_num = 0;

typedef struct {
    /* data */
    void (*task_func)(void);
    uint32_t rate_ms;
    uint32_t last_run;
}task_t;


task_t scheduler_task[] = {
    {uart_task,10,0},
    {cam_task,5,0},
    {Cam_Pid_Task,50,0}
};


void scheduler_init(void)
{
    task_num = sizeof(scheduler_task)/sizeof(task_t);
    HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_4);
    user_uart_init();//串口初始化
    Step_Motor_Init();//步进电机初始化
    PID_Init();
}

void scheduler_run(void)
{
    //循环遍历任务
    for(uint8_t i = 0;i < task_num;i++)
    {
        //获取当前系统时间
        uint32_t now_time = uwTick;
        //判断当前任务是否执行
        if(scheduler_task[i].rate_ms + scheduler_task[i].last_run <= now_time)
        {
            //更新任务上次运行时间
            scheduler_task[i].last_run = now_time;
            //指向当前任务
            scheduler_task[i].task_func();
        }
    }
}


